package com.vrv.common.chart.builder;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.SeriesSelection;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import com.vrv.common.chart.AbstractAChartEngine;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

public class PieChartBuilder extends AbstractAChartEngine {

    public String getName() {
        return null;
    }

    public String getDesc() {
        return null;
    }

    /**
     * Executes the chart demo.
     *
     * @param context
     *            the context
     * @return the built intent
     */
    public View buildChartView(Context context) {

        String caption = "设备信息统计图表";
        String[] labels = new String[] { "设备总数", "应注册", "已注册", "在线设备", "安装杀毒" };// x轴的值
        Number[] values = new Number[] { 120, 14, 11, 10, 190 };// 相应的y轴的值
        int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN };// 图例颜色决定线条颜色

        DefaultRenderer renderer = buildCategoryRenderer(colors);

        renderer.setChartTitle(caption);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsColor(Color.WHITE);
        renderer.setBackgroundColor(Color.BLACK);
        renderer.setApplyBackgroundColor(true);
        renderer.setShowLabels(true);
        renderer.setZoomEnabled(true);
        renderer.setDisplayValues(true);
        renderer.setClickEnabled(true);
        renderer.setMargins(new int[] { 40, 40, 40, 40 });// 可以设置和legend之间的距离

        // SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);
        GraphicalView view = ChartFactory.getPieChartView(context, buildCategoryDataset(caption, labels, values), renderer);
        view.setOnClickListener(new PieChartClickListener(view, renderer, values.length));
        return view;
    }

    private class PieChartClickListener implements View.OnClickListener {

        private GraphicalView view = null;
        private DefaultRenderer renderer = null;
        private int itemCount = 0;

        public PieChartClickListener(GraphicalView view, DefaultRenderer renderer, int itemCount) {
            this.view = view;
            this.renderer = renderer;
            this.itemCount = itemCount;
        }

        @Override
        public void onClick(View v) {
            SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
            if (seriesSelection != null) {
                for (int i = 0; i < itemCount; i++) {
                    SimpleSeriesRenderer r = renderer.getSeriesRendererAt(i);
                    // 处理渐变
                    r.setGradientEnabled(i == seriesSelection.getPointIndex());
                    r.setGradientStart(0, Color.LTGRAY);
                    r.setGradientStop(0, r.getColor());
                    r.setHighlighted(i == seriesSelection.getPointIndex());
                }
                view.repaint();
            }
        }
    }

}
